Question 1:

library(data.table)
# read 2004 data 
data_2004 <- fread("data/data_2004.csv")
pm_2004 <- data.table(data_2004, keep.rownames=FALSE, check.names=FALSE, key=NULL, stringsAsFactors=FALSE)

# read 2019 data 
data_2019 <- fread("data/data_2019.csv")
pm_2019 <- data.table(data_2019, keep.rownames=FALSE, check.names=FALSE, key=NULL, stringsAsFactors=FALSE)
# check 2004 data

# check the dimension
dim(pm_2004)
## [1] 19233    20
# check the header
head(pm_2004)
##          Date Source  Site ID POC Daily Mean PM2.5 Concentration    UNITS
## 1: 01/01/2004    AQS 60010007   1                            8.9 ug/m3 LC
## 2: 01/02/2004    AQS 60010007   1                           12.2 ug/m3 LC
## 3: 01/03/2004    AQS 60010007   1                           16.5 ug/m3 LC
## 4: 01/04/2004    AQS 60010007   1                           19.5 ug/m3 LC
## 5: 01/05/2004    AQS 60010007   1                           11.5 ug/m3 LC
## 6: 01/06/2004    AQS 60010007   1                           32.5 ug/m3 LC
##    DAILY_AQI_VALUE Site Name DAILY_OBS_COUNT PERCENT_COMPLETE
## 1:              37 Livermore               1              100
## 2:              51 Livermore               1              100
## 3:              60 Livermore               1              100
## 4:              67 Livermore               1              100
## 5:              48 Livermore               1              100
## 6:              94 Livermore               1              100
##    AQS_PARAMETER_CODE                     AQS_PARAMETER_DESC CBSA_CODE
## 1:              88101               PM2.5 - Local Conditions     41860
## 2:              88502 Acceptable PM2.5 AQI & Speciation Mass     41860
## 3:              88502 Acceptable PM2.5 AQI & Speciation Mass     41860
## 4:              88502 Acceptable PM2.5 AQI & Speciation Mass     41860
## 5:              88502 Acceptable PM2.5 AQI & Speciation Mass     41860
## 6:              88502 Acceptable PM2.5 AQI & Speciation Mass     41860
##                            CBSA_NAME STATE_CODE      STATE COUNTY_CODE  COUNTY
## 1: San Francisco-Oakland-Hayward, CA          6 California           1 Alameda
## 2: San Francisco-Oakland-Hayward, CA          6 California           1 Alameda
## 3: San Francisco-Oakland-Hayward, CA          6 California           1 Alameda
## 4: San Francisco-Oakland-Hayward, CA          6 California           1 Alameda
## 5: San Francisco-Oakland-Hayward, CA          6 California           1 Alameda
## 6: San Francisco-Oakland-Hayward, CA          6 California           1 Alameda
##    SITE_LATITUDE SITE_LONGITUDE
## 1:      37.68753      -121.7842
## 2:      37.68753      -121.7842
## 3:      37.68753      -121.7842
## 4:      37.68753      -121.7842
## 5:      37.68753      -121.7842
## 6:      37.68753      -121.7842
# check the footer
tail(pm_2004)
##          Date Source  Site ID POC Daily Mean PM2.5 Concentration    UNITS
## 1: 12/14/2004    AQS 61131003   1                             11 ug/m3 LC
## 2: 12/17/2004    AQS 61131003   1                             16 ug/m3 LC
## 3: 12/20/2004    AQS 61131003   1                             17 ug/m3 LC
## 4: 12/23/2004    AQS 61131003   1                              9 ug/m3 LC
## 5: 12/26/2004    AQS 61131003   1                             24 ug/m3 LC
## 6: 12/29/2004    AQS 61131003   1                              9 ug/m3 LC
##    DAILY_AQI_VALUE            Site Name DAILY_OBS_COUNT PERCENT_COMPLETE
## 1:              46 Woodland-Gibson Road               1              100
## 2:              59 Woodland-Gibson Road               1              100
## 3:              61 Woodland-Gibson Road               1              100
## 4:              38 Woodland-Gibson Road               1              100
## 5:              76 Woodland-Gibson Road               1              100
## 6:              38 Woodland-Gibson Road               1              100
##    AQS_PARAMETER_CODE       AQS_PARAMETER_DESC CBSA_CODE
## 1:              88101 PM2.5 - Local Conditions     40900
## 2:              88101 PM2.5 - Local Conditions     40900
## 3:              88101 PM2.5 - Local Conditions     40900
## 4:              88101 PM2.5 - Local Conditions     40900
## 5:              88101 PM2.5 - Local Conditions     40900
## 6:              88101 PM2.5 - Local Conditions     40900
##                                  CBSA_NAME STATE_CODE      STATE COUNTY_CODE
## 1: Sacramento--Roseville--Arden-Arcade, CA          6 California         113
## 2: Sacramento--Roseville--Arden-Arcade, CA          6 California         113
## 3: Sacramento--Roseville--Arden-Arcade, CA          6 California         113
## 4: Sacramento--Roseville--Arden-Arcade, CA          6 California         113
## 5: Sacramento--Roseville--Arden-Arcade, CA          6 California         113
## 6: Sacramento--Roseville--Arden-Arcade, CA          6 California         113
##    COUNTY SITE_LATITUDE SITE_LONGITUDE
## 1:   Yolo      38.66121      -121.7327
## 2:   Yolo      38.66121      -121.7327
## 3:   Yolo      38.66121      -121.7327
## 4:   Yolo      38.66121      -121.7327
## 5:   Yolo      38.66121      -121.7327
## 6:   Yolo      38.66121      -121.7327
# check the variable name
colnames(pm_2004)
##  [1] "Date"                           "Source"                        
##  [3] "Site ID"                        "POC"                           
##  [5] "Daily Mean PM2.5 Concentration" "UNITS"                         
##  [7] "DAILY_AQI_VALUE"                "Site Name"                     
##  [9] "DAILY_OBS_COUNT"                "PERCENT_COMPLETE"              
## [11] "AQS_PARAMETER_CODE"             "AQS_PARAMETER_DESC"            
## [13] "CBSA_CODE"                      "CBSA_NAME"                     
## [15] "STATE_CODE"                     "STATE"                         
## [17] "COUNTY_CODE"                    "COUNTY"                        
## [19] "SITE_LATITUDE"                  "SITE_LONGITUDE"
# check the variable type 
str(pm_2004)
## Classes 'data.table' and 'data.frame':   19233 obs. of  20 variables:
##  $ Date                          : chr  "01/01/2004" "01/02/2004" "01/03/2004" "01/04/2004" ...
##  $ Source                        : chr  "AQS" "AQS" "AQS" "AQS" ...
##  $ Site ID                       : int  60010007 60010007 60010007 60010007 60010007 60010007 60010007 60010007 60010007 60010007 ...
##  $ POC                           : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ Daily Mean PM2.5 Concentration: num  8.9 12.2 16.5 19.5 11.5 32.5 15.5 29.9 21 15.7 ...
##  $ UNITS                         : chr  "ug/m3 LC" "ug/m3 LC" "ug/m3 LC" "ug/m3 LC" ...
##  $ DAILY_AQI_VALUE               : int  37 51 60 67 48 94 58 88 70 59 ...
##  $ Site Name                     : chr  "Livermore" "Livermore" "Livermore" "Livermore" ...
##  $ DAILY_OBS_COUNT               : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ PERCENT_COMPLETE              : num  100 100 100 100 100 100 100 100 100 100 ...
##  $ AQS_PARAMETER_CODE            : int  88101 88502 88502 88502 88502 88502 88502 88502 88502 88101 ...
##  $ AQS_PARAMETER_DESC            : chr  "PM2.5 - Local Conditions" "Acceptable PM2.5 AQI & Speciation Mass" "Acceptable PM2.5 AQI & Speciation Mass" "Acceptable PM2.5 AQI & Speciation Mass" ...
##  $ CBSA_CODE                     : int  41860 41860 41860 41860 41860 41860 41860 41860 41860 41860 ...
##  $ CBSA_NAME                     : chr  "San Francisco-Oakland-Hayward, CA" "San Francisco-Oakland-Hayward, CA" "San Francisco-Oakland-Hayward, CA" "San Francisco-Oakland-Hayward, CA" ...
##  $ STATE_CODE                    : int  6 6 6 6 6 6 6 6 6 6 ...
##  $ STATE                         : chr  "California" "California" "California" "California" ...
##  $ COUNTY_CODE                   : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ COUNTY                        : chr  "Alameda" "Alameda" "Alameda" "Alameda" ...
##  $ SITE_LATITUDE                 : num  37.7 37.7 37.7 37.7 37.7 ...
##  $ SITE_LONGITUDE                : num  -122 -122 -122 -122 -122 ...
##  - attr(*, ".internal.selfref")=<externalptr>
# check the summary
summary(pm_2004)
##      Date              Source             Site ID              POC        
##  Length:19233       Length:19233       Min.   :60010007   Min.   : 1.000  
##  Class :character   Class :character   1st Qu.:60370002   1st Qu.: 1.000  
##  Mode  :character   Mode  :character   Median :60658001   Median : 1.000  
##                                        Mean   :60588026   Mean   : 1.816  
##                                        3rd Qu.:60750006   3rd Qu.: 2.000  
##                                        Max.   :61131003   Max.   :12.000  
##                                                                           
##  Daily Mean PM2.5 Concentration    UNITS           DAILY_AQI_VALUE 
##  Min.   : -0.10                 Length:19233       Min.   :  0.00  
##  1st Qu.:  6.00                 Class :character   1st Qu.: 25.00  
##  Median : 10.10                 Mode  :character   Median : 42.00  
##  Mean   : 13.13                                    Mean   : 46.32  
##  3rd Qu.: 16.30                                    3rd Qu.: 60.00  
##  Max.   :251.00                                    Max.   :301.00  
##                                                                    
##   Site Name         DAILY_OBS_COUNT PERCENT_COMPLETE AQS_PARAMETER_CODE
##  Length:19233       Min.   :1       Min.   :100      Min.   :88101     
##  Class :character   1st Qu.:1       1st Qu.:100      1st Qu.:88101     
##  Mode  :character   Median :1       Median :100      Median :88101     
##                     Mean   :1       Mean   :100      Mean   :88267     
##                     3rd Qu.:1       3rd Qu.:100      3rd Qu.:88502     
##                     Max.   :1       Max.   :100      Max.   :88502     
##                                                                        
##  AQS_PARAMETER_DESC   CBSA_CODE      CBSA_NAME           STATE_CODE
##  Length:19233       Min.   :12540   Length:19233       Min.   :6   
##  Class :character   1st Qu.:31080   Class :character   1st Qu.:6   
##  Mode  :character   Median :40140   Mode  :character   Median :6   
##                     Mean   :35328                      Mean   :6   
##                     3rd Qu.:41860                      3rd Qu.:6   
##                     Max.   :49700                      Max.   :6   
##                     NA's   :1253                                   
##     STATE            COUNTY_CODE        COUNTY          SITE_LATITUDE  
##  Length:19233       Min.   :  1.00   Length:19233       Min.   :32.63  
##  Class :character   1st Qu.: 37.00   Class :character   1st Qu.:34.07  
##  Mode  :character   Median : 65.00   Mode  :character   Median :36.48  
##                     Mean   : 58.63                      Mean   :36.23  
##                     3rd Qu.: 75.00                      3rd Qu.:38.10  
##                     Max.   :113.00                      Max.   :41.71  
##                                                                        
##  SITE_LONGITUDE  
##  Min.   :-124.2  
##  1st Qu.:-121.6  
##  Median :-119.3  
##  Mean   :-119.7  
##  3rd Qu.:-117.9  
##  Max.   :-115.5  
## 

summary of the finding

For pm_2004 data table, there are 19233 rows and 20 columns with 20 variables. For all those variables, there are 8 variables with data type “chr”, 8 variables with data type “int”, and 4 variables with data type “num”. The mean for PM2.5 Concentration in 2004 is 13.13, and the mean of daily air quality index value is 46.32

# check 2019 data

# check the dimension
dim(pm_2019)
## [1] 53156    20
# check the header
head(pm_2019)
##          Date Source  Site ID POC Daily Mean PM2.5 Concentration    UNITS
## 1: 01/01/2019    AQS 60010007   3                            5.7 ug/m3 LC
## 2: 01/02/2019    AQS 60010007   3                           11.9 ug/m3 LC
## 3: 01/03/2019    AQS 60010007   3                           20.1 ug/m3 LC
## 4: 01/04/2019    AQS 60010007   3                           28.8 ug/m3 LC
## 5: 01/05/2019    AQS 60010007   3                           11.2 ug/m3 LC
## 6: 01/06/2019    AQS 60010007   3                            2.7 ug/m3 LC
##    DAILY_AQI_VALUE Site Name DAILY_OBS_COUNT PERCENT_COMPLETE
## 1:              24 Livermore               1              100
## 2:              50 Livermore               1              100
## 3:              68 Livermore               1              100
## 4:              86 Livermore               1              100
## 5:              47 Livermore               1              100
## 6:              11 Livermore               1              100
##    AQS_PARAMETER_CODE       AQS_PARAMETER_DESC CBSA_CODE
## 1:              88101 PM2.5 - Local Conditions     41860
## 2:              88101 PM2.5 - Local Conditions     41860
## 3:              88101 PM2.5 - Local Conditions     41860
## 4:              88101 PM2.5 - Local Conditions     41860
## 5:              88101 PM2.5 - Local Conditions     41860
## 6:              88101 PM2.5 - Local Conditions     41860
##                            CBSA_NAME STATE_CODE      STATE COUNTY_CODE  COUNTY
## 1: San Francisco-Oakland-Hayward, CA          6 California           1 Alameda
## 2: San Francisco-Oakland-Hayward, CA          6 California           1 Alameda
## 3: San Francisco-Oakland-Hayward, CA          6 California           1 Alameda
## 4: San Francisco-Oakland-Hayward, CA          6 California           1 Alameda
## 5: San Francisco-Oakland-Hayward, CA          6 California           1 Alameda
## 6: San Francisco-Oakland-Hayward, CA          6 California           1 Alameda
##    SITE_LATITUDE SITE_LONGITUDE
## 1:      37.68753      -121.7842
## 2:      37.68753      -121.7842
## 3:      37.68753      -121.7842
## 4:      37.68753      -121.7842
## 5:      37.68753      -121.7842
## 6:      37.68753      -121.7842
# check the footer
tail(pm_2019)
##          Date Source  Site ID POC Daily Mean PM2.5 Concentration    UNITS
## 1: 11/11/2019    AQS 61131003   1                           13.5 ug/m3 LC
## 2: 11/17/2019    AQS 61131003   1                           18.1 ug/m3 LC
## 3: 11/29/2019    AQS 61131003   1                           12.5 ug/m3 LC
## 4: 12/17/2019    AQS 61131003   1                           23.8 ug/m3 LC
## 5: 12/23/2019    AQS 61131003   1                            1.0 ug/m3 LC
## 6: 12/29/2019    AQS 61131003   1                            9.1 ug/m3 LC
##    DAILY_AQI_VALUE            Site Name DAILY_OBS_COUNT PERCENT_COMPLETE
## 1:              54 Woodland-Gibson Road               1              100
## 2:              64 Woodland-Gibson Road               1              100
## 3:              52 Woodland-Gibson Road               1              100
## 4:              76 Woodland-Gibson Road               1              100
## 5:               4 Woodland-Gibson Road               1              100
## 6:              38 Woodland-Gibson Road               1              100
##    AQS_PARAMETER_CODE       AQS_PARAMETER_DESC CBSA_CODE
## 1:              88101 PM2.5 - Local Conditions     40900
## 2:              88101 PM2.5 - Local Conditions     40900
## 3:              88101 PM2.5 - Local Conditions     40900
## 4:              88101 PM2.5 - Local Conditions     40900
## 5:              88101 PM2.5 - Local Conditions     40900
## 6:              88101 PM2.5 - Local Conditions     40900
##                                  CBSA_NAME STATE_CODE      STATE COUNTY_CODE
## 1: Sacramento--Roseville--Arden-Arcade, CA          6 California         113
## 2: Sacramento--Roseville--Arden-Arcade, CA          6 California         113
## 3: Sacramento--Roseville--Arden-Arcade, CA          6 California         113
## 4: Sacramento--Roseville--Arden-Arcade, CA          6 California         113
## 5: Sacramento--Roseville--Arden-Arcade, CA          6 California         113
## 6: Sacramento--Roseville--Arden-Arcade, CA          6 California         113
##    COUNTY SITE_LATITUDE SITE_LONGITUDE
## 1:   Yolo      38.66121      -121.7327
## 2:   Yolo      38.66121      -121.7327
## 3:   Yolo      38.66121      -121.7327
## 4:   Yolo      38.66121      -121.7327
## 5:   Yolo      38.66121      -121.7327
## 6:   Yolo      38.66121      -121.7327
# check the variable name
colnames(pm_2019)
##  [1] "Date"                           "Source"                        
##  [3] "Site ID"                        "POC"                           
##  [5] "Daily Mean PM2.5 Concentration" "UNITS"                         
##  [7] "DAILY_AQI_VALUE"                "Site Name"                     
##  [9] "DAILY_OBS_COUNT"                "PERCENT_COMPLETE"              
## [11] "AQS_PARAMETER_CODE"             "AQS_PARAMETER_DESC"            
## [13] "CBSA_CODE"                      "CBSA_NAME"                     
## [15] "STATE_CODE"                     "STATE"                         
## [17] "COUNTY_CODE"                    "COUNTY"                        
## [19] "SITE_LATITUDE"                  "SITE_LONGITUDE"
# check the variable type 
str(pm_2019)
## Classes 'data.table' and 'data.frame':   53156 obs. of  20 variables:
##  $ Date                          : chr  "01/01/2019" "01/02/2019" "01/03/2019" "01/04/2019" ...
##  $ Source                        : chr  "AQS" "AQS" "AQS" "AQS" ...
##  $ Site ID                       : int  60010007 60010007 60010007 60010007 60010007 60010007 60010007 60010007 60010007 60010007 ...
##  $ POC                           : int  3 3 3 3 3 3 3 3 3 3 ...
##  $ Daily Mean PM2.5 Concentration: num  5.7 11.9 20.1 28.8 11.2 2.7 2.8 7 3.1 7.1 ...
##  $ UNITS                         : chr  "ug/m3 LC" "ug/m3 LC" "ug/m3 LC" "ug/m3 LC" ...
##  $ DAILY_AQI_VALUE               : int  24 50 68 86 47 11 12 29 13 30 ...
##  $ Site Name                     : chr  "Livermore" "Livermore" "Livermore" "Livermore" ...
##  $ DAILY_OBS_COUNT               : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ PERCENT_COMPLETE              : num  100 100 100 100 100 100 100 100 100 100 ...
##  $ AQS_PARAMETER_CODE            : int  88101 88101 88101 88101 88101 88101 88101 88101 88101 88101 ...
##  $ AQS_PARAMETER_DESC            : chr  "PM2.5 - Local Conditions" "PM2.5 - Local Conditions" "PM2.5 - Local Conditions" "PM2.5 - Local Conditions" ...
##  $ CBSA_CODE                     : int  41860 41860 41860 41860 41860 41860 41860 41860 41860 41860 ...
##  $ CBSA_NAME                     : chr  "San Francisco-Oakland-Hayward, CA" "San Francisco-Oakland-Hayward, CA" "San Francisco-Oakland-Hayward, CA" "San Francisco-Oakland-Hayward, CA" ...
##  $ STATE_CODE                    : int  6 6 6 6 6 6 6 6 6 6 ...
##  $ STATE                         : chr  "California" "California" "California" "California" ...
##  $ COUNTY_CODE                   : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ COUNTY                        : chr  "Alameda" "Alameda" "Alameda" "Alameda" ...
##  $ SITE_LATITUDE                 : num  37.7 37.7 37.7 37.7 37.7 ...
##  $ SITE_LONGITUDE                : num  -122 -122 -122 -122 -122 ...
##  - attr(*, ".internal.selfref")=<externalptr>
# check the summary
summary(pm_2019)
##      Date              Source             Site ID              POC        
##  Length:53156       Length:53156       Min.   :60010007   Min.   : 1.000  
##  Class :character   Class :character   1st Qu.:60310004   1st Qu.: 1.000  
##  Mode  :character   Mode  :character   Median :60612003   Median : 3.000  
##                                        Mean   :60565264   Mean   : 2.573  
##                                        3rd Qu.:60771002   3rd Qu.: 3.000  
##                                        Max.   :61131003   Max.   :21.000  
##                                                                           
##  Daily Mean PM2.5 Concentration    UNITS           DAILY_AQI_VALUE 
##  Min.   : -2.200                Length:53156       Min.   :  0.00  
##  1st Qu.:  4.000                Class :character   1st Qu.: 17.00  
##  Median :  6.500                Mode  :character   Median : 27.00  
##  Mean   :  7.738                                   Mean   : 30.57  
##  3rd Qu.:  9.900                                   3rd Qu.: 41.00  
##  Max.   :120.900                                   Max.   :185.00  
##                                                                    
##   Site Name         DAILY_OBS_COUNT PERCENT_COMPLETE AQS_PARAMETER_CODE
##  Length:53156       Min.   :1       Min.   :100      Min.   :88101     
##  Class :character   1st Qu.:1       1st Qu.:100      1st Qu.:88101     
##  Mode  :character   Median :1       Median :100      Median :88101     
##                     Mean   :1       Mean   :100      Mean   :88214     
##                     3rd Qu.:1       3rd Qu.:100      3rd Qu.:88502     
##                     Max.   :1       Max.   :100      Max.   :88502     
##                                                                        
##  AQS_PARAMETER_DESC   CBSA_CODE      CBSA_NAME           STATE_CODE
##  Length:53156       Min.   :12540   Length:53156       Min.   :6   
##  Class :character   1st Qu.:31080   Class :character   1st Qu.:6   
##  Mode  :character   Median :40140   Mode  :character   Median :6   
##                     Mean   :35839                      Mean   :6   
##                     3rd Qu.:41860                      3rd Qu.:6   
##                     Max.   :49700                      Max.   :6   
##                     NA's   :4181                                   
##     STATE            COUNTY_CODE        COUNTY          SITE_LATITUDE  
##  Length:53156       Min.   :  1.00   Length:53156       Min.   :32.58  
##  Class :character   1st Qu.: 31.00   Class :character   1st Qu.:34.14  
##  Mode  :character   Median : 61.00   Mode  :character   Median :36.63  
##                     Mean   : 56.38                      Mean   :36.34  
##                     3rd Qu.: 77.00                      3rd Qu.:37.97  
##                     Max.   :113.00                      Max.   :41.76  
##                                                                        
##  SITE_LONGITUDE  
##  Min.   :-124.2  
##  1st Qu.:-121.6  
##  Median :-119.8  
##  Mean   :-119.8  
##  3rd Qu.:-118.1  
##  Max.   :-115.5  
## 

summary of the finding

For pm_2019 data table, there are 53156 rows and 20 columns with 20 variables. For all those variables, there are 8 variables with data type “chr”, 8 variables with data type “int”, and 4 variables with data type “num”. The mean for PM2.5 Concentration in 2019 is 7.738, and the mean of daily air quality index value is 30.57

Question 2:

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:data.table':
## 
##     between, first, last
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# Combine the two years of data into one data frame
pm <- rbind(pm_2004, pm_2019)

# create a new column for year, which will serve as an identifier
pm <- mutate(pm, year = factor(rep(c(2004, 2019), c(nrow(pm_2004), nrow(pm_2019)))))%>%
  rename(lat = SITE_LATITUDE, lon = SITE_LONGITUDE, pm2.5 = "Daily Mean PM2.5 Concentration")

Question 3:

library(leaflet)

#filter locations and year of the table to create a new table
pm_new <- pm
pm_new = pm_new%>%
  select(lat,lon,year)

# Generating a color palette
pm.pal <- colorFactor(c('blue','red'), domain=pm_new$year)

# Create a basic map in leaflet() that shows the locations of the sites
pmmap <- leaflet(pm_new) %>% 
  # The looks of the Map
  addProviderTiles('CartoDB.Positron') %>% 
  # Some circles
  addCircles(
    lat = ~lat, lng=~lon,
    label = ~paste0(year, ' C'), color = ~ pm.pal(year),
    opacity = 1, fillOpacity = 1, radius = 2
    ) %>%
  # And a pretty legend
  addLegend('bottomleft', pal=pm.pal, values=pm_new$year,
          title='Year')

pmmap

Summarize the spatial distribution of the monitoring sites

I find that there are more locations in 2019 than in 2004. The sites have more spread across the California in 2019 than in 2004.

Question 4:

# Check for any missing values of PM in the combined dataset
apply(is.na(pm), 2, which)
## $Date
## integer(0)
## 
## $Source
## integer(0)
## 
## $`Site ID`
## integer(0)
## 
## $POC
## integer(0)
## 
## $pm2.5
## integer(0)
## 
## $UNITS
## integer(0)
## 
## $DAILY_AQI_VALUE
## integer(0)
## 
## $`Site Name`
## integer(0)
## 
## $DAILY_OBS_COUNT
## integer(0)
## 
## $PERCENT_COMPLETE
## integer(0)
## 
## $AQS_PARAMETER_CODE
## integer(0)
## 
## $AQS_PARAMETER_DESC
## integer(0)
## 
## $CBSA_CODE
##    [1]   903   904   905   906   907   908   909   910   911   912   913   914
##   [13]   915   916   917   918   919   920   921   922   923   924   925   926
##   [25]   927   928   929   930   931   932   933   934   935   936   937   938
##   [37]   939   940   941   942   943   944   945   946   947   948   949   950
##   [49]   951   952   953   954   955   956   957   958   959   960   961   962
##   [61]   963   964   965   966   967   968   969   970   971   972   973   974
##   [73]   975   976   977   978   979   980   981   982   983   984   985   986
##   [85]   987   988   989   990   991   992   993   994   995   996   997   998
##   [97]   999  1000  1001  1002  1003  1004  1005  1006  1007  1008  1009  1010
##  [109]  1011  1012  1013  1014  1015  1016  1017  1018  1019  1020  1021  1022
##  [121]  1023  1024  1025  1026  1027  1028  1029  1030  1031  1032  1033  1034
##  [133]  1035  1036  1037  1038  1039  1040  1041  1042  1043  1044  1045  1046
##  [145]  1047  1048  1049  1050  1051  1052  1053  1054  1055  1056  1057  1058
##  [157]  1059  1060  1061  1062  1063  1064  1065  1066  1067  1068  1069  1070
##  [169]  1071  1072  1073  1074  1075  1076  1077  1078  1079  1080  1081  1082
##  [181]  1083  1084  1085  1086  1087  1088  1089  1090  1091  1092  1093  1094
##  [193]  1095  1096  1097  1098  1099  1100  1101  1102  1103  1104  1105  1106
##  [205]  1107  1108  1109  1110  1111  1112  1113  1114  1115  1116  1117  1118
##  [217]  1119  1120  1121  1122  1123  1124  1125  1126  1127  1128  1129  1130
##  [229]  1131  1132  1133  1134  1135  1136  1137  1138  1139  1140  1141  1142
##  [241]  1143  1144  1145  1146  1147  1148  1149  1150  1151  1152  1153  1154
##  [253]  1155  1156  1157  1158  1159  1160  1161  1162  1163  1164  6962  6963
##  [265]  6964  6965  6966  6967  6968  6969  6970  6971  6972  6973  6974  6975
##  [277]  6976  6977  6978  6979  6980  6981  6982  6983  6984  6985  6986  6987
##  [289]  6988  6989  6990  6991  6992  6993  6994  6995  6996  6997  6998  6999
##  [301]  7000  7001  7002  7003  7004  7005  7006  7007  7008  7009  7010  7011
##  [313]  7012  7013  7014  7015  7016  7017  7018  7019  7020  7021  7022  7023
##  [325]  7024  7025  7026  7027  7028  7029  7030  7031  7032  7033  7034  7035
##  [337]  7036  7037  7038  7039  7040  7041  7042  7043  7044  7045  7046  7047
##  [349]  7048  7049  7050  7051  7052  7053  7054  7055  7056  7057  7058  7059
##  [361]  7060  7061  7062  7063  7064  7065  7066  7067  7068  7069  7070  7071
##  [373]  7072  7073  7074  7075  7076  7077  7078  7079  7080  7081  7082  7083
##  [385]  7084  7085  7086  7087  7088  7089  7090  7091  7092  7093  7094  7095
##  [397]  7096  7097  7098  7099  7100  7101  7102  7103  7104  7105  7106  7107
##  [409]  7108  7109  7110  7111  7112  7113  7114  7115  7116  7117  7118  7119
##  [421]  7120  7121  7122  7123  7124  7125  7126  7127  7128  7129  7130  7131
##  [433]  7132  7133  7134  7135  7136  7137  7138  7139  7140  7141  7142  7143
##  [445]  7144  7145  7146  7147  7148  7149  7150  7151  7152  7153  7154  7155
##  [457]  7156  7157  7158  7159  7160  7161  7162  7163  7164  7165  7166  7167
##  [469]  7168  7169  7170  7171  7172  7173  7174  7175  7176  7177  7178  7179
##  [481]  7180  7181  7182  7183  7184  7185  7186  7187  7188  7189  7190  7191
##  [493]  7192  7193  7194  7195  7196  7197  7198  7199  7200  7201  7202  7203
##  [505]  7204  7205  7206  7207  7208  7209  7210  7211  7212  7213  7214  7215
##  [517]  7216  7217  7218  7219  7220  7221  7222  7223  7224  7225  7226  7227
##  [529]  7228  7229  7230  7231  7232  7233  7234  7235  7236  7237  7238  7239
##  [541]  7240  7241  7242  7243  7244  7245  7246  7247  7248  7249  7250  7251
##  [553]  7252  7253  7254  7255  7256  7257  7258  7259  7260  7261  7262  7263
##  [565]  7264  7265  7266  7267  7268  7269  7270  7271  7272  7273  7274  7275
##  [577]  7276  7277  7278  7279  7280  7281  7433  7434  7435  7436  7437  7438
##  [589]  7439  7440  7441  7442  7443  7444  7445  7446  7447  7448  7449  7450
##  [601]  7451  7452  7453  7454  7455  7456  7457  7458  7459  7460  7461  7462
##  [613]  7463  7464  7465  7466  7467  7468  7469  7470  7471  7472  7473  7474
##  [625]  7475  7476  7477  7478  7479  7480  7481  7482  7483  7484  7485  7486
##  [637]  7487  7488  7489  7490  7491  7492  7493  7494  7495  7496  7497  7498
##  [649]  7499  7500  7501  7502  7503  7504  7505  7506  7507  7508  7509  7510
##  [661]  7511  7512  7513  7514  7515  7516  7517  7518  7519  7520  7521  7522
##  [673]  7523  7524  7525  7526  7527  7528  7529  7530  7531  7532  7533  7534
##  [685]  7535  7536  7537  7538  7539  7540  7541  7542  7543  7544  7545  7546
##  [697]  7547  7548  7549  7550  7551  7552  7553  7554  7555  7556  7557  7558
##  [709]  7559  7560  7561  7562  7563  7564  7565  7566  7567  7568  7569  7570
##  [721]  7571  7572  7573  7574  7575  7576  7577  7578  7579  7580  7581  7582
##  [733]  7583  7584  7585  7586  7587  7588  7589  7590  7591  7592  7593  7594
##  [745]  7595  7596  7597  7598  7599  7600  7601  7602  7603  7604  7605  7606
##  [757]  7607  7608  7609  7610  7611  7612  7613  8681  8682  8683  8684  8685
##  [769]  8686  8687  8688  8689  8690  8691  8692  8693  8694  8695  8696  8697
##  [781]  8698  8699  8700  8701  8702  8703  8704  8705  8706  8707  8708  8709
##  [793]  8710  8711  8712  8713  8714  8715  8716  8717  8718  8719  8720  8721
##  [805]  8722  8723  8724  8725  8726  8727  8728  8729  8730  8731  8732  8733
##  [817]  8734  8735  8736  8737  8738  8739  8740  8741  8742  8743  8744  8745
##  [829]  8746  8747  8748  8749  8750  8751  8752  8753  8754  8755  8756  8757
##  [841]  8758  8759  8760  8761  8762  8763  8764  8765  8766  8767  8768  8769
##  [853]  8770  8771  8772  8773  8774  8775  8776  8777  8778  8779  8780  8781
##  [865]  8782  8783  8784  8785  8786  8787  8788  8789  8790  8791  8792  8793
##  [877]  8794  8795  8796  8797  8798  8799  8800  8801  8802  8803  8804  8805
##  [889]  8806  8807  8808  8809  8810  8811  8812  8813  8814  8815  8816  8817
##  [901]  8818  8819  8820  8821  8822  8823  8824  8825  8826  8827  8828  8829
##  [913]  8830  8831  8832  8833  8834  8835  8836  8837  8838  8839  8840  8841
##  [925]  8842  8843  8844  8845  8846  8847  8848  8849  8850  8851  8852  8853
##  [937]  8854  8855  8856  8857  8858  8859  8860  8861  8862  8863  8864  8865
##  [949]  8866  8867  8868  8869  8870  8871  8872  8873  8874  8875  8876  8877
##  [961]  8878  8879  8880  8881  8882  8883  8884  8885  8886  8887  8888  8889
##  [973]  8890  8891  8892  8893  8894  8895  8896  8897  8898  8899  8900  8901
##  [985]  8902  8903  8904  8905  8906  8907  8908  8909  8910  8911  8912  8913
##  [997]  8914  8915  8916  8917  8918  8919  8920  8921  8922  8923  8924  8925
## [1009]  8926  8927  8928  8929  8930  8931  8932  8933  8934  8935  8936  8937
## [1021]  8938 16375 16376 16377 16378 16379 16380 16381 16382 16383 16384 16385
## [1033] 16386 16387 16388 16389 16390 16391 16392 16393 16394 16395 16396 16397
## [1045] 16398 16399 16400 16401 16402 16403 16404 16405 16406 16407 16408 16409
## [1057] 16410 16411 16412 16413 16414 16415 16416 16417 16418 16419 16420 16421
## [1069] 16422 16423 16424 16425 16426 16427 16428 16429 16430 16431 16432 16433
## [1081] 16434 16435 16436 16437 16438 16439 16440 16441 16442 16443 16444 16445
## [1093] 16446 16447 16448 16449 16450 16451 16452 16453 16454 16455 16456 16457
## [1105] 16458 16459 16460 16461 16462 16463 16464 16465 16466 16467 16468 16469
## [1117] 16470 16471 16472 16473 16474 16475 16476 16477 16478 16479 16480 16481
## [1129] 16482 16483 16484 16485 16486 16487 16488 16489 16490 16491 17437 17438
## [1141] 17439 17440 17441 17442 17443 17444 17445 17446 17447 17448 17449 17450
## [1153] 17451 17452 17453 17454 17455 17456 17457 17458 17459 17460 17461 17462
## [1165] 17463 17464 17465 17466 17467 17468 17469 17470 17471 17472 17473 17474
## [1177] 17475 17476 17477 17478 17479 17480 17481 17482 17483 17484 17485 17486
## [1189] 17487 17488 17489 17490 17491 17492 17493 17494 17495 17496 17497 17498
## [1201] 17499 17500 17501 17502 17503 17504 17505 17506 17507 17508 17509 17510
## [1213] 17511 17512 17513 17514 17515 17516 17517 17518 17519 17520 17521 17522
## [1225] 17523 17524 17525 17526 17527 17528 17529 17530 17531 17532 17533 17534
## [1237] 17535 17536 17537 17538 17539 17540 17541 17542 17543 17544 17545 17546
## [1249] 17547 17548 17549 17550 17551 22625 22626 22627 22628 22629 22630 22631
## [1261] 22632 22633 22634 22635 22636 22637 22638 22639 22640 22641 22642 22643
## [1273] 22644 22645 22646 22647 22648 22649 22650 22651 22652 22653 22654 22655
## [1285] 22656 22657 22658 22659 22660 22661 22662 22663 22664 22665 22666 22667
## [1297] 22668 22669 22670 22671 22672 22673 22674 22675 22676 22677 22678 22679
## [1309] 22680 22681 22682 22683 22684 22685 22686 22687 22688 22689 22690 22691
## [1321] 22692 22693 22694 22695 22696 22697 22698 22699 22700 22701 22702 22703
## [1333] 22704 22705 22706 22707 22708 22709 22710 22711 22712 22713 22714 22715
## [1345] 22716 22717 22718 22719 22720 22721 22722 22723 22724 22725 22726 22727
## [1357] 22728 22729 22730 22731 22732 22733 22734 22735 22736 22737 22738 22739
## [1369] 22740 22741 22742 22743 22744 22745 22746 22747 22748 22749 22750 22751
## [1381] 22752 22753 22754 22755 22756 22757 22758 22759 22760 22761 22762 22763
## [1393] 22764 22765 22766 22767 22768 22769 22770 22771 22772 22773 22774 22775
## [1405] 22776 22777 22778 22779 22780 22781 22782 22783 22784 22785 22786 22787
## [1417] 22788 22789 22790 22791 22792 22793 22794 22795 22796 22797 22798 22799
## [1429] 22800 22801 22802 22803 22804 22805 22806 22807 22808 22809 22810 22811
## [1441] 22812 22813 22814 22815 22816 22817 22818 22819 22820 22821 22822 22823
## [1453] 22824 22825 22826 22827 22828 22829 22830 22831 22832 22833 22834 22835
## [1465] 22836 22837 22838 22839 22840 22841 22842 22843 22844 22845 22846 22847
## [1477] 22848 22849 22850 22851 22852 22853 22854 22855 22856 22857 22858 22859
## [1489] 22860 22861 22862 22863 22864 22865 22866 22867 22868 22869 22870 22871
## [1501] 22872 22873 22874 22875 22876 22877 22878 22879 22880 22881 22882 22883
## [1513] 22884 22885 22886 22887 22888 22889 22890 22891 22892 22893 22894 22895
## [1525] 22896 22897 22898 22899 22900 22901 22902 22903 22904 22905 22906 22907
## [1537] 22908 22909 22910 22911 22912 22913 22914 22915 22916 22917 22918 22919
## [1549] 22920 22921 22922 22923 22924 22925 22926 22927 22928 22929 22930 22931
## [1561] 22932 22933 22934 22935 22936 22937 22938 22939 22940 22941 22942 22943
## [1573] 22944 22945 22946 22947 22948 22949 22950 22951 22952 22953 22954 22955
## [1585] 22956 22957 22958 22959 22960 22961 22962 22963 22964 22965 22966 22967
## [1597] 22968 22969 22970 22971 22972 22973 22974 22975 22976 22977 22978 22979
## [1609] 22980 22981 22982 22983 22984 22985 22986 22987 22988 22989 22990 22991
## [1621] 22992 22993 22994 22995 22996 22997 22998 22999 23000 23001 23002 23003
## [1633] 23004 23005 23006 23007 23008 23009 23010 23011 23012 23013 23014 23015
## [1645] 23016 23017 23018 23019 23020 23021 23022 23023 23024 23025 23026 23027
## [1657] 23028 23029 23030 23031 23032 23033 23034 23035 23036 23037 23038 23039
## [1669] 23040 23041 23042 23043 23044 23045 23046 23047 23048 23049 23050 23051
## [1681] 23052 23053 23054 23055 23056 23057 23058 23059 23060 23061 23062 23063
## [1693] 23064 23065 23066 23067 23068 23069 23070 23071 23072 23073 23074 23075
## [1705] 23076 23077 23078 23079 23080 23081 23082 23083 23084 23085 23086 23087
## [1717] 23088 23089 23090 23091 23092 23093 23094 23095 23096 23097 23098 23099
## [1729] 23100 23101 23102 23103 23104 23105 23106 23107 23108 23109 23110 23111
## [1741] 23112 23113 23114 23115 23116 23117 23118 23119 23120 23121 23122 23123
## [1753] 23124 23125 23126 23127 23128 23129 23130 23131 23132 23133 23134 23135
## [1765] 23136 23137 23138 23139 23140 23141 23142 23143 23144 23145 23146 23147
## [1777] 23148 23149 23150 23151 23152 23153 23154 23155 23156 23157 23158 23159
## [1789] 23160 23161 23162 23163 23164 23165 23166 23167 23168 23169 23170 23171
## [1801] 23172 23173 23174 23175 23176 23177 23178 23179 23180 23181 23182 23183
## [1813] 23184 23185 23186 23187 23188 23189 23190 23191 23192 23193 23194 23195
## [1825] 23196 23197 23198 23199 23200 23201 23202 23203 23204 23205 23206 23207
## [1837] 23208 23209 23210 23211 23212 23213 23214 23215 23216 23217 23218 23219
## [1849] 23220 23221 23222 23223 23224 23225 23226 23227 23228 23229 23230 23231
## [1861] 23232 23233 23234 23235 23236 23237 23238 23239 23240 23241 23242 23243
## [1873] 23244 23245 23246 23247 23248 23249 23250 23251 23252 23253 23254 23255
## [1885] 23256 23257 23258 23259 23260 23261 23262 23263 23264 23265 23266 23267
## [1897] 23268 23269 23270 23271 23272 23273 23274 23275 23276 23277 23278 23279
## [1909] 23280 23281 23282 23283 23284 23285 23286 23287 23288 23289 23290 23291
## [1921] 23292 23293 23294 23295 23296 23297 23298 23299 23300 23301 23302 23303
## [1933] 23304 23305 23306 23307 23308 23309 23310 23311 23312 23313 23314 23315
## [1945] 23316 23317 23318 23319 23320 23321 23322 23323 23324 23325 23326 23327
## [1957] 23328 23329 23330 23331 23332 23333 23334 23335 23336 23337 23338 23339
## [1969] 23340 23341 23342 23343 23344 23345 23346 23347 23348 23349 23350 23351
## [1981] 23352 23353 23354 23355 23356 23357 23358 23359 23360 23361 23362 23363
## [1993] 23364 23365 23366 23367 23368 23369 23370 23371 23372 23373 23374 23375
## [2005] 23376 23377 23378 23379 23380 23381 23382 23383 23384 23385 23386 23387
## [2017] 23388 23389 23390 23391 23392 23393 23394 23395 23396 23397 23398 23399
## [2029] 23400 23401 23402 23403 23404 23405 23406 23407 23408 23409 23410 23411
## [2041] 23412 23413 23414 23415 23416 23417 23418 23419 23420 23421 23422 23423
## [2053] 23424 23425 23426 23427 23428 23429 23430 23431 23432 23433 23434 23435
## [2065] 23436 23437 23438 23439 23440 23441 23442 23443 23444 23445 23446 23447
## [2077] 23448 23449 23450 23451 23452 23453 23454 23455 23456 23457 23458 23459
## [2089] 23460 23461 23462 23463 23464 23465 23466 23467 23468 23469 23470 23471
## [2101] 23472 23473 23474 23475 23476 23477 23478 23479 23480 23481 23482 23483
## [2113] 23484 23485 23486 23487 23488 23489 23490 23491 23492 23493 23494 23495
## [2125] 23496 23497 23498 23499 23500 23501 23502 23503 23504 23505 23506 23507
## [2137] 23508 23509 23510 23511 23512 23513 23514 23515 23516 23517 23518 23519
## [2149] 23520 23521 23522 23523 23524 23525 23526 23527 23528 23529 23530 23531
## [2161] 23532 23533 23534 23535 23536 23537 23538 23539 23540 23541 23542 23543
## [2173] 23544 23545 23546 23547 23548 23549 23550 23551 23552 23553 23554 23555
## [2185] 23556 23557 23558 23559 23560 23561 23562 23563 23564 23565 23566 23567
## [2197] 23568 23569 23570 23571 23572 23573 23574 23575 23576 23577 23578 23579
## [2209] 23580 23581 23582 23583 23584 23585 23586 23587 23588 23589 23590 23591
## [2221] 23592 23593 23594 23595 23596 23597 23598 23599 23600 23601 23602 23603
## [2233] 23604 23605 23606 23607 23608 23609 23610 23611 23612 23613 23614 23615
## [2245] 23616 23617 23618 23619 23620 23621 23622 23623 23624 23625 23626 23627
## [2257] 23628 23629 23630 23631 23632 23633 23634 23635 23636 23637 23638 23639
## [2269] 23640 23641 23642 23643 23644 23645 23646 23647 23648 23649 23650 23651
## [2281] 23652 23653 23654 23655 23656 23657 23658 23659 23660 23661 23662 23663
## [2293] 23664 23665 23666 23667 23668 23669 23670 23671 23672 23673 23674 23675
## [2305] 23676 23677 23678 23679 23680 23681 23682 23683 23684 23685 23686 23687
## [2317] 23688 23689 23690 27659 27660 27661 27662 27663 27664 27665 27666 27667
## [2329] 27668 27669 27670 27671 27672 27673 27674 27675 27676 27677 27678 27679
## [2341] 27680 27681 27682 27683 27684 27685 27686 27687 27688 27689 27690 27691
## [2353] 27692 27693 27694 27695 27696 27697 27698 27699 27700 27701 27702 27703
## [2365] 27704 27705 27706 27707 27708 27709 27710 27711 27712 27713 27714 27715
## [2377] 27716 27717 27718 27719 27720 27721 27722 27723 27724 27725 27726 27727
## [2389] 27728 27729 27730 27731 27732 27733 27734 27735 27736 27737 27738 27739
## [2401] 27740 27741 27742 27743 27744 27745 27746 27747 27748 27749 27750 27751
## [2413] 27752 27753 27754 27755 27756 27757 27758 27759 27760 27761 27762 27763
## [2425] 27764 27765 27766 27767 27768 27769 27770 27771 27772 27773 27774 27775
## [2437] 27776 27777 27778 27779 27780 27781 27782 27783 27784 27785 27786 27787
## [2449] 27788 27789 27790 27791 27792 27793 27794 27795 27796 27797 27798 27799
## [2461] 27800 27801 27802 27803 27804 27805 27806 27807 27808 27809 27810 27811
## [2473] 27812 27813 27814 27815 27816 27817 27818 27819 27820 27821 27822 27823
## [2485] 27824 27825 27826 27827 27828 27829 27830 27831 27832 27833 27834 27835
## [2497] 27836 27837 27838 27839 27840 27841 27842 27843 27844 27845 27846 27847
## [2509] 27848 27849 27850 27851 27852 27853 27854 27855 27856 27857 27858 27859
## [2521] 27860 27861 27862 27863 27864 27865 27866 27867 27868 27869 27870 27871
## [2533] 27872 27873 27874 27875 27876 27877 27878 27879 27880 27881 27882 27883
## [2545] 27884 27885 27886 27887 27888 27889 27890 27891 27892 27893 27894 27895
## [2557] 27896 27897 27898 27899 27900 27901 27902 27903 27904 27905 27906 27907
## [2569] 27908 27909 27910 27911 27912 27913 27914 27915 27916 27917 27918 27919
## [2581] 27920 27921 27922 27923 27924 27925 27926 27927 27928 27929 27930 27931
## [2593] 27932 27933 27934 27935 27936 27937 27938 27939 27940 27941 27942 27943
## [2605] 27944 27945 27946 27947 27948 27949 27950 27951 27952 27953 27954 27955
## [2617] 27956 27957 27958 27959 27960 27961 27962 27963 27964 27965 27966 27967
## [2629] 27968 27969 27970 27971 27972 27973 27974 27975 27976 27977 27978 27979
## [2641] 27980 27981 27982 27983 27984 27985 27986 27987 27988 27989 27990 27991
## [2653] 27992 27993 27994 27995 27996 27997 27998 27999 28000 28001 28002 28003
## [2665] 28004 28005 28006 28007 28008 28009 28010 28011 28012 28013 28014 38711
## [2677] 38712 38713 38714 38715 38716 38717 38718 38719 38720 38721 38722 38723
## [2689] 38724 38725 38726 38727 38728 38729 38730 38731 38732 38733 38734 38735
## [2701] 38736 38737 38738 38739 38740 38741 38742 38743 38744 38745 38746 38747
## [2713] 38748 38749 38750 38751 38752 38753 38754 38755 38756 38757 38758 38759
## [2725] 38760 38761 38762 38763 38764 38765 38766 38767 38768 38769 38770 38771
## [2737] 38772 38773 38774 38775 38776 38777 38778 38779 38780 38781 38782 38783
## [2749] 38784 38785 38786 38787 38788 38789 38790 38791 38792 38793 38794 38795
## [2761] 38796 38797 38798 38799 38800 38801 38802 38803 38804 38805 38806 38807
## [2773] 38808 38809 38810 38811 38812 38813 38814 38815 38816 38817 38818 38819
## [2785] 38820 38821 38822 38823 38824 38825 38826 38827 38828 38829 38830 38831
## [2797] 38832 38833 38834 38835 38836 38837 38838 38839 38840 38841 38842 38843
## [2809] 38844 38845 38846 38847 38848 38849 38850 38851 38852 38853 38854 38855
## [2821] 38856 38857 38858 38859 38860 38861 38862 38863 38864 38865 38866 38867
## [2833] 38868 38869 38870 38871 38872 38873 38874 38875 38876 38877 38878 38879
## [2845] 38880 38881 38882 38883 38884 38885 38886 38887 38888 38889 38890 38891
## [2857] 38892 38893 38894 38895 38896 38897 38898 38899 38900 38901 38902 38903
## [2869] 38904 38905 38906 38907 38908 38909 38910 38911 38912 38913 38914 38915
## [2881] 38916 38917 38918 38919 38920 38921 38922 38923 38924 38925 38926 38927
## [2893] 38928 38929 38930 38931 38932 38933 38934 38935 38936 38937 38938 38939
## [2905] 38940 38941 38942 38943 38944 38945 38946 38947 38948 38949 38950 38951
## [2917] 38952 38953 38954 38955 38956 38957 38958 38959 38960 38961 38962 38963
## [2929] 38964 38965 38966 38967 38968 38969 38970 38971 38972 38973 38974 38975
## [2941] 38976 38977 38978 38979 38980 38981 38982 38983 38984 38985 38986 38987
## [2953] 38988 38989 38990 38991 38992 38993 38994 38995 38996 38997 38998 38999
## [2965] 39000 39001 39002 39003 39004 39005 39006 39007 39008 39009 39010 39011
## [2977] 39012 39013 39014 39015 39016 39017 39018 39019 39020 39021 39022 39023
## [2989] 39024 39025 39026 39027 39028 39029 39030 39031 39032 39033 39034 39035
## [3001] 39036 39037 39038 39039 39040 39041 39042 39043 39044 39045 39046 39047
## [3013] 39048 39049 39050 39051 39052 39053 39054 39055 39056 39057 39058 39059
## [3025] 39060 39061 39062 39063 39064 39065 39066 39067 39068 39069 39070 39071
## [3037] 39072 39073 39074 39075 39076 39077 39078 39079 39080 39081 39082 39083
## [3049] 39084 39085 39086 39087 39088 39089 39090 39091 39092 39093 39094 39095
## [3061] 39096 39097 39098 39099 39100 39101 39102 39103 39104 39105 39106 39107
## [3073] 39108 39109 39110 39111 39112 39113 39114 39115 39116 39117 39118 39119
## [3085] 39120 39121 39122 39123 39124 39125 39126 39127 39128 39129 39130 39131
## [3097] 39132 39133 39134 39135 39136 39137 39138 39139 39140 39141 39142 39143
## [3109] 39144 39145 39146 39147 39148 39149 39150 39151 39152 39153 39154 39155
## [3121] 39156 39157 39158 39159 39160 39161 39162 39163 39164 39165 39166 39167
## [3133] 39168 39169 39170 39171 39172 39173 39174 39175 39176 39177 39178 39179
## [3145] 39180 39181 39182 39183 39184 39185 39186 39187 39188 39189 39190 39191
## [3157] 39192 39193 39194 39195 39196 39197 39198 39199 39200 39201 39202 39203
## [3169] 39204 39205 39206 39207 39208 39209 39210 39211 39212 39213 39214 39215
## [3181] 39216 39217 39218 39219 39220 39221 39222 39223 39224 39225 39226 39227
## [3193] 39228 39229 39230 39231 39232 39233 39234 39235 39236 39237 39238 39239
## [3205] 39240 39241 39242 39243 39244 39245 39246 40439 40440 40441 40442 40443
## [3217] 40444 40445 40446 40447 40448 40449 40450 40451 40452 40453 40454 40455
## [3229] 40456 40457 40458 40459 40460 40461 40462 40463 40464 40465 40466 40467
## [3241] 40468 40469 40470 40471 40472 40473 40474 40475 40476 40477 40478 40479
## [3253] 40480 40481 40482 40483 40484 40485 40486 40487 40488 40489 40490 40491
## [3265] 40492 40493 40494 40495 40496 40497 40498 40499 40500 40501 40502 40503
## [3277] 40504 40505 40506 40507 40508 40509 40510 40511 40512 40513 40514 40515
## [3289] 40516 40517 40518 40519 40520 40521 40522 40523 40524 40525 40526 40527
## [3301] 40528 40529 40530 40531 40532 40533 40534 40535 40536 40537 40538 40539
## [3313] 40540 40541 40542 40543 40544 40545 40546 40547 40548 40549 40550 40551
## [3325] 40552 40553 40554 40555 40556 40557 40558 40559 40560 40561 40562 40563
## [3337] 40564 40565 40566 40567 40568 40569 40570 40571 40572 40573 40574 40575
## [3349] 40576 40577 40578 40579 40580 40581 40582 40583 40584 40585 40586 40587
## [3361] 40588 40589 40590 40591 40592 40593 40594 40595 40596 40597 40598 40599
## [3373] 40600 40601 40602 40603 40604 40605 40606 40607 40608 40609 40610 40611
## [3385] 40612 40613 40614 40615 40616 40617 40618 40619 40620 40621 40622 40623
## [3397] 40624 40625 40626 40627 40628 40629 40630 40631 40632 40633 40634 40635
## [3409] 40636 40637 40638 40639 40640 40641 40642 40643 40644 40645 40646 40647
## [3421] 40648 40649 40650 40651 40652 40653 40654 40655 40656 40657 40658 40659
## [3433] 40660 40661 40662 40663 40664 40665 40666 40667 40668 40669 40670 40671
## [3445] 40672 40673 40674 40675 40676 40677 40678 40679 40680 40681 40682 40683
## [3457] 40684 40685 40686 40687 40688 40689 40690 40691 40692 40693 40694 40695
## [3469] 40696 40697 40698 40699 40700 40701 40702 40703 40704 40705 40706 40707
## [3481] 40708 40709 40710 40711 40712 40713 40714 40715 40716 40717 40718 40719
## [3493] 40720 40721 40722 40723 40724 40725 40726 40727 40728 40729 40730 40731
## [3505] 40732 40733 40734 40735 40736 40737 40738 40739 40740 40741 40742 40743
## [3517] 40744 40745 40746 40747 40748 40749 40750 40751 40752 40753 40754 40755
## [3529] 40756 40757 40758 40759 40760 40761 40762 40763 40764 40765 40766 40767
## [3541] 40768 40769 40770 40771 40772 40773 40774 40775 40776 40777 40778 40779
## [3553] 40780 40781 40782 40783 40784 40785 40786 40787 40788 40789 40790 40791
## [3565] 40792 40793 40794 40795 40796 40797 40798 40799 40800 40801 40802 40803
## [3577] 40804 40805 40806 40807 40808 40809 40810 40811 40812 40813 40814 40815
## [3589] 40816 40817 40818 40819 40820 40821 40822 40823 40824 40825 40826 40827
## [3601] 40828 40829 40830 40831 40832 40833 40834 40835 40836 40837 40838 40839
## [3613] 40840 40841 40842 40843 40844 40845 40846 40847 40848 40849 40850 40851
## [3625] 40852 40853 40854 40855 40856 40857 40858 40859 40860 40861 40862 40863
## [3637] 40864 40865 40866 40867 40868 40869 40870 40871 40872 40873 40874 40875
## [3649] 40876 40877 40878 40879 40880 40881 40882 40883 40884 40885 40886 40887
## [3661] 40888 40889 40890 40891 40892 40893 40894 40895 40896 40897 40898 40899
## [3673] 40900 40901 40902 40903 40904 40905 40906 40907 40908 40909 40910 40911
## [3685] 40912 40913 40914 40915 40916 40917 40918 40919 40920 40921 40922 40923
## [3697] 40924 40925 40926 40927 40928 40929 40930 40931 40932 40933 40934 40935
## [3709] 40936 40937 40938 40939 40940 40941 40942 40943 40944 40945 40946 40947
## [3721] 40948 40949 40950 40951 40952 40953 40954 40955 40956 40957 40958 40959
## [3733] 40960 40961 40962 40963 40964 40965 40966 40967 40968 40969 40970 40971
## [3745] 40972 40973 40974 40975 40976 40977 40978 40979 40980 40981 40982 40983
## [3757] 40984 40985 40986 40987 40988 40989 40990 40991 40992 40993 40994 40995
## [3769] 40996 40997 40998 40999 41000 41001 41002 41003 41004 41005 41006 41007
## [3781] 41008 41009 41010 41011 41012 41013 41014 41015 41016 41017 41018 41019
## [3793] 41020 41021 41022 41023 41024 41025 41026 41027 41028 41029 41030 41031
## [3805] 41032 41033 41034 41035 41036 41037 41038 41039 41040 41041 41042 41043
## [3817] 41044 41045 41046 41047 41048 41049 41050 41051 41052 41053 41054 41055
## [3829] 41056 41057 41058 41059 41060 41061 41062 41063 41064 41065 41066 41067
## [3841] 41068 41069 41070 41071 41072 41073 41074 41075 41076 41077 41078 41079
## [3853] 41080 41081 41082 41083 41084 41085 41086 41087 41088 41089 41090 41091
## [3865] 41092 41093 41094 41095 41096 41097 41098 41099 41100 41101 41102 41103
## [3877] 41104 41105 41106 41107 41108 41109 41110 41111 41112 41113 41114 41115
## [3889] 41116 41117 41118 41119 41120 41121 41122 41123 41124 41125 41126 41127
## [3901] 41128 41129 41130 41131 41132 41133 41134 41135 41136 41137 41138 41139
## [3913] 41140 41141 41142 41143 41144 41145 41146 41147 41148 41149 41150 41151
## [3925] 41152 41153 41154 41155 41156 41157 41158 41159 41160 41161 41162 41163
## [3937] 41164 41165 41166 41167 41168 41169 41170 41171 41172 41173 41174 41175
## [3949] 41176 41177 41178 41179 41180 41181 41182 41183 41184 41185 41186 41187
## [3961] 41188 41189 41190 41191 41192 41193 41194 41195 41196 41197 41198 41199
## [3973] 41200 41201 41202 41203 41204 41205 41206 41207 41208 41209 41210 41211
## [3985] 41212 41213 41214 41215 41216 41217 41218 41219 41220 41221 41222 41223
## [3997] 41224 41225 41226 41227 41228 41229 41230 41231 41232 41233 41234 41235
## [4009] 41236 41237 41238 41239 41240 41241 41242 41243 41244 41245 41246 41247
## [4021] 41248 41249 41250 41251 41252 41253 41254 41255 41256 41257 41258 41259
## [4033] 41260 41261 41262 41263 41264 41265 41266 41267 41268 41269 41270 41271
## [4045] 41272 41273 41274 41275 41276 41277 41278 41279 41280 41281 41282 41283
## [4057] 41284 41285 41286 41287 41288 41289 41290 41291 41292 41293 41294 41295
## [4069] 41296 41297 41298 41299 41300 41301 41302 41303 41304 41305 41306 41307
## [4081] 41308 41309 41310 41311 41312 41313 41314 41315 41316 41317 41318 41319
## [4093] 41320 41321 41322 41323 41324 41325 41326 41327 41328 45904 45905 45906
## [4105] 45907 45908 45909 45910 45911 45912 45913 45914 45915 45916 45917 45918
## [4117] 45919 45920 45921 45922 45923 45924 45925 45926 45927 45928 45929 45930
## [4129] 45931 45932 45933 45934 45935 45936 45937 45938 45939 45940 45941 45942
## [4141] 45943 45944 45945 45946 45947 45948 45949 45950 45951 45952 45953 45954
## [4153] 45955 45956 45957 45958 45959 45960 45961 45962 45963 45964 45965 45966
## [4165] 45967 45968 45969 45970 45971 45972 45973 45974 45975 45976 45977 45978
## [4177] 45979 45980 45981 45982 45983 45984 45985 45986 45987 45988 45989 45990
## [4189] 45991 45992 45993 45994 45995 45996 45997 45998 45999 46000 46001 46002
## [4201] 46003 46004 46005 46006 46007 46008 46009 46010 46011 46012 46013 46014
## [4213] 46015 46016 46017 46018 46019 46020 46021 46022 46023 46024 46025 46026
## [4225] 46027 46028 46029 46030 46031 46032 46033 46034 46035 46036 46037 46038
## [4237] 46039 46040 46041 46042 46043 46044 46045 46046 46047 46048 46049 46050
## [4249] 46051 46052 46053 46054 46055 46056 46057 46058 46059 46060 46061 46062
## [4261] 46063 46064 46065 46066 46067 46068 46069 46070 46071 46072 46073 46074
## [4273] 46075 46076 46077 46078 46079 46080 46081 46082 46083 46084 46085 46086
## [4285] 46087 46088 46089 46090 46091 46092 46093 46094 46095 46096 46097 46098
## [4297] 46099 46100 46101 46102 46103 46104 46105 46106 46107 46108 46109 46110
## [4309] 46111 46112 46113 46114 46115 46116 46117 46118 46119 46120 46121 46122
## [4321] 46123 46124 46125 46126 46127 46128 46129 46130 46131 46132 46133 46134
## [4333] 46135 46136 46137 46138 46139 46140 46141 46142 46143 46144 46145 46146
## [4345] 46147 46148 46149 46150 46151 46152 46153 46154 46155 46156 46157 46158
## [4357] 46159 46160 46161 46162 46163 46164 46165 46166 46167 46168 46169 46170
## [4369] 46171 46172 46173 46174 46175 46176 46177 46178 46179 46180 46181 46182
## [4381] 46183 46184 46185 46186 46187 46188 46189 46190 46191 46192 46193 46194
## [4393] 46195 46196 46197 46198 46199 46200 46201 46202 46203 46204 46205 46206
## [4405] 46207 46208 46209 46210 46211 46212 46213 46214 46215 46216 46217 46218
## [4417] 46219 46220 46221 46222 46223 46224 46225 46226 46227 46228 46229 46230
## [4429] 46231 46232 46233 46234 46235 46236 46237 46238 46239 46240 46241 46242
## [4441] 46243 46244 46245 46246 46247 46248 46249 46250 46251 46252 46253 46254
## [4453] 46255 46256 46257 46258 46259 46260 46261 46262 46263 46264 46265 46266
## [4465] 46267 46268 46269 46270 46271 46272 46273 46274 46275 46276 46277 46278
## [4477] 46279 46280 46281 46282 46283 46284 46285 46286 46287 46288 46289 46290
## [4489] 46291 46292 46293 46294 46295 46296 46297 46298 46299 46300 46301 46302
## [4501] 46303 46304 46305 46306 46307 46308 46309 46310 46311 46312 46313 46314
## [4513] 46315 46316 46317 46318 46319 46320 46321 46322 46323 46324 46325 46326
## [4525] 46327 46328 46329 46330 46331 46332 46333 46334 46335 46336 46337 46338
## [4537] 46339 46340 46341 46342 46343 46344 46345 46346 46347 46348 46349 46350
## [4549] 46351 46352 46353 46354 46355 46356 46357 46358 46359 46360 46361 46362
## [4561] 46363 46364 46365 46366 46367 46368 46369 46370 46371 46372 46373 46374
## [4573] 46375 46376 46377 46378 46379 46380 46381 46382 46383 46384 46385 46386
## [4585] 46387 46388 46389 46390 46391 46392 46393 46394 46395 46396 46397 46398
## [4597] 46399 46400 46401 46402 46403 46404 46405 46406 46407 46408 46409 46410
## [4609] 46411 46412 46413 46414 46415 46416 46417 46418 46419 46420 46421 46422
## [4621] 46423 46424 46425 46426 46427 46428 46429 46430 46431 46432 46433 65164
## [4633] 65165 65166 65167 65168 65169 65170 65171 65172 65173 65174 65175 65176
## [4645] 65177 65178 65179 65180 65181 65182 65183 65184 65185 65186 65187 65188
## [4657] 65189 65190 65191 65192 65193 65194 65195 65196 65197 65198 65199 65200
## [4669] 65201 65202 65203 65204 65205 65206 65207 65208 65209 65210 65211 65212
## [4681] 65213 65214 65215 65216 65217 65218 65219 65220 65221 65222 65223 65224
## [4693] 65225 65226 65227 65228 65229 65230 65231 65232 65233 65234 65235 65236
## [4705] 65237 65238 65239 65240 65241 65242 65243 65244 65245 65246 65247 65248
## [4717] 65249 65250 65251 65252 65253 65254 65255 65256 65257 65258 65259 65260
## [4729] 65261 65262 65263 65264 65265 65266 65267 65268 65269 65270 65271 65272
## [4741] 65273 65274 65275 65276 65277 65278 65279 65280 65281 65282 65283 65284
## [4753] 65285 65286 65287 65288 65289 65290 65291 65292 65293 65294 65295 65296
## [4765] 65297 65298 65299 65300 65301 65302 65303 65304 65305 65306 65307 65308
## [4777] 65309 65310 65311 65312 65313 65314 65315 65316 65317 65318 65319 65320
## [4789] 65321 65322 65323 65324 65325 65326 65327 65328 65329 65330 65331 65332
## [4801] 65333 65334 65335 65336 65337 65338 65339 65340 65341 65342 65343 65344
## [4813] 65345 65346 65347 65348 65349 65350 65351 65352 65353 65354 65355 65356
## [4825] 65357 65358 65359 65360 65361 65362 65363 65364 65365 65366 65367 65368
## [4837] 65369 65370 65371 65372 65373 65374 65375 65376 65377 65378 65379 65380
## [4849] 65381 65382 65383 65384 65385 65386 65387 65388 65389 65390 65391 65392
## [4861] 65393 65394 65395 65396 65397 65398 65399 65400 65401 65402 65403 65404
## [4873] 65405 65406 65407 65408 65409 65410 65411 65412 65413 65414 65415 65416
## [4885] 65417 65418 65419 65420 65421 65422 65423 65424 65425 65426 65427 65428
## [4897] 65429 65430 65431 65432 65433 65434 65435 65436 65437 65438 65439 65440
## [4909] 65441 65442 65443 65444 65445 65446 65447 65448 65449 65450 65451 65452
## [4921] 65453 65454 65455 65456 65457 65458 65459 65460 65461 65462 65463 65464
## [4933] 65465 65466 65467 65468 65469 65470 65471 65472 65473 65474 65475 65476
## [4945] 65477 65478 65479 65480 65481 65482 65483 65484 65485 65486 65487 65488
## [4957] 65489 65490 65491 65492 65493 65494 65495 65496 65497 65498 65499 65500
## [4969] 65501 65502 65503 65504 65505 65506 65507 65508 65509 65510 65511 65512
## [4981] 65513 65514 65515 65516 65517 65518 65519 65520 65521 65522 65523 65524
## [4993] 65525 65526 65527 65528 65529 65530 65531 65532 65533 65534 65535 65536
## [5005] 65537 65538 65539 65540 65541 65542 65543 65544 65545 65546 65547 65548
## [5017] 65549 65550 65551 65552 65553 65554 65555 65556 65557 65558 65559 65560
## [5029] 65561 65562 65563 65564 65565 65566 65567 65568 65569 65570 65571 65572
## [5041] 65573 65574 65575 65576 65577 65578 65579 65580 65581 65582 65583 65584
## [5053] 65585 65586 65587 65588 65589 65590 65591 65592 65593 65594 65595 65596
## [5065] 65597 65598 65599 65600 65601 65602 65603 65604 65605 65606 65607 65608
## [5077] 65609 65610 65611 65612 65613 65614 65615 65616 65617 65618 65619 65620
## [5089] 65621 68540 68541 68542 68543 68544 68545 68546 68547 68548 68549 68550
## [5101] 68551 68552 68553 68554 68555 68556 68557 68558 68559 68560 68561 68562
## [5113] 68563 68564 68565 68566 68567 68568 68569 68570 68571 68572 68573 68574
## [5125] 68575 68576 68577 68578 68579 68580 68581 68582 68583 68584 68585 68586
## [5137] 68587 68588 68589 68590 68591 68592 68593 68594 68595 68596 68597 68598
## [5149] 68599 68600 68601 68602 68603 68604 68605 68606 68607 68608 68609 68610
## [5161] 68611 68612 68613 68614 68615 68616 68617 68618 68619 68620 68621 68622
## [5173] 68623 68624 68625 68626 68627 68628 68629 68630 68631 68632 68633 68634
## [5185] 68635 68636 68637 68638 68639 68640 68641 68642 68643 68644 68645 68646
## [5197] 68647 68648 68649 68650 68651 68652 68653 68654 68655 68656 68657 68658
## [5209] 68659 68660 68661 68662 68663 68664 68665 68666 68667 68668 68669 68670
## [5221] 68671 68672 68673 68674 68675 68676 68677 68678 68679 68680 68681 68682
## [5233] 68683 68684 68685 68686 68687 68688 68689 68690 68691 68692 68693 68694
## [5245] 68695 68696 68697 68698 68699 68700 68701 68702 68703 68704 68705 68706
## [5257] 68707 68708 68709 68710 68711 68712 68713 68714 68715 68716 68717 68718
## [5269] 68719 68720 68721 68722 68723 68724 68725 68726 68727 68728 68729 68730
## [5281] 68731 68732 68733 68734 68735 68736 68737 68738 68739 68740 68741 68742
## [5293] 68743 68744 68745 68746 68747 68748 68749 68750 68751 68752 68753 68754
## [5305] 68755 68756 68757 68758 68759 68760 68761 68762 68763 68764 68765 68766
## [5317] 68767 68768 68769 68770 68771 68772 68773 68774 68775 68776 68777 68778
## [5329] 68779 68780 68781 68782 68783 68784 68785 68786 68787 68788 68789 68790
## [5341] 68791 68792 68793 68794 68795 68796 68797 68798 68799 68800 68801 68802
## [5353] 68803 68804 68805 68806 68807 68808 68809 68810 68811 68812 68813 68814
## [5365] 68815 68816 68817 68818 68819 68820 68821 68822 68823 68824 68825 68826
## [5377] 68827 68828 68829 68830 68831 68832 68833 68834 68835 68836 68837 68838
## [5389] 68839 68840 68841 68842 68843 68844 68845 68846 68847 68848 68849 68850
## [5401] 68851 68852 68853 68854 68855 68856 68857 68858 68859 68860 68861 68862
## [5413] 68863 68864 68865 68866 68867 68868 68869 68870 68871 68872 68873 68874
## [5425] 68875 68876 68877 68878 68879 68880 68881 68882 68883 68884
## 
## $CBSA_NAME
## integer(0)
## 
## $STATE_CODE
## integer(0)
## 
## $STATE
## integer(0)
## 
## $COUNTY_CODE
## integer(0)
## 
## $COUNTY
## integer(0)
## 
## $lat
## integer(0)
## 
## $lon
## integer(0)
## 
## $year
## integer(0)
# Check for any implausible values of PM in the combined dataset
apply(is.nan(as.matrix(pm)), 2, which)
## integer(0)

There is no implausible data where there has some missing data. All the missing data are in the column called CBSA_CODE (Core Based Statistical Areas). In addition, if CDSA_CODE is missing, CDSA_NAME is missing too.

# create a new data frame that only contains missing data
missing_data <- filter(pm, CBSA_NAME == "") %>% 
         select("Site Name", CBSA_CODE, CBSA_NAME, STATE, COUNTY, year)%>% 
         unique

We see that there are only 10 CBSA sites data missing in 2004, whereas in 2019, there are 15 code and name missing since in 2019, there are more individual monitors. All the missing CBSA sites in 2004 are also missing in 2019 except for Portola-161 Nevada Street. It may have many reasons to explain why even though 15 years passed, CBSA_code is still missing. However, since we do not have further data base, it is hard to tell the specific reason.

Question 5:

Analysis trend based on state level

library(ggplot2)

set.seed(2015)
idx <- sample(nrow(pm), 1000)
qplot(year, pm2.5, data = pm[idx, ], geom = "boxplot")

From the boxplot, it seems that on average, the levels of PM2.5 in 2019 are lower than they were in 2004. Interestingly, there also appears to be much greater variation in PM2.5 in 2019 than there was in 2004

# do statistical analysis
with(pm, tapply(pm2.5, year, summary))
## $`2004`
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   -0.10    6.00   10.10   13.13   16.30  251.00 
## 
## $`2019`
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  -2.200   4.000   6.500   7.738   9.900 120.900

It shows that both in 2004 and 2019, there are some negative values which could not occur in reality. Since the minimum value in 2004 is just -0.1 close to 0, so we just further explore data in 2019 only.

# select nagative values in 2019 data
filter(pm, year == "2019") %>% summarize(negative = mean(pm2.5 < 0, na.rm = TRUE))
##     negative
## 1 0.00530514
# get the portion of negatives values and extract the month from each of the dates with negative values and attempt to identify when negative values occur most often
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:data.table':
## 
##     hour, isoweek, mday, minute, month, quarter, second, wday, week,
##     yday, year
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
negative <- filter(pm, year == "2019") %>% 
         mutate(negative = pm2.5 < 0, date = as.Date(Date)) %>%
         select(date, negative)

mutate(negative, month = factor(month.name[month(date)], levels = month.name)) %>%
         group_by(month) %>%
         summarize(pct.negative = mean(negative, na.rm = TRUE) * 100)
## # A tibble: 13 × 2
##    month     pct.negative
##    <fct>            <dbl>
##  1 January          1.03 
##  2 February         0.656
##  3 March            0.995
##  4 April            0.670
##  5 May              0.612
##  6 June             0.334
##  7 July             0.861
##  8 August           0.553
##  9 September        0.388
## 10 October          0.618
## 11 November         0.559
## 12 December         0.390
## 13 <NA>             0.463

After doing analysis, we find that the negative is only a small portion of data and the negative value occurs the most often in January. There is not further validation to say why January contains more negative value. So we just ignore those negative values from row.

Analysis trend based on county level

# calculate the mean of PM2.5 for each county in 2004 and 2019
mn <- group_by(pm, year, COUNTY) %>% summarize(pm2.5 = mean(pm2.5, na.rm = TRUE))
## `summarise()` has grouped output by 'year'. You can override using the
## `.groups` argument.
head(mn)
## # A tibble: 6 × 3
## # Groups:   year [1]
##   year  COUNTY       pm2.5
##   <fct> <chr>        <dbl>
## 1 2004  Alameda      11.1 
## 2 2004  Butte        10.1 
## 3 2004  Calaveras     7.61
## 4 2004  Colusa       10.0 
## 5 2004  Contra Costa 12.8 
## 6 2004  Del Norte     3.41
tail(mn)
## # A tibble: 6 × 3
## # Groups:   year [1]
##   year  COUNTY  pm2.5
##   <fct> <chr>   <dbl>
## 1 2019  Sutter   8.27
## 2 2019  Tehama   5.43
## 3 2019  Trinity  4.91
## 4 2019  Tulare  11.2 
## 5 2019  Ventura  6.65
## 6 2019  Yolo     6.85
# make the plot to show mean and draw a line connecting the means for each year in the same county to highlight the trend
qplot(year, pm2.5, data = mutate(mn, year = as.numeric(as.character(year))), 
       color = factor(COUNTY), 
       geom = c("point", "line"))

From the graph, we find that most of counties decrease their PM2.5 concentration from 2004 to 2019, even though there are still few counties increase the PM2.5 concentration.

Analysis trend based on Site in Los Angeles level

# identify sites in Los Angeles that has data in 2004 and 2019
sites <- filter(pm, COUNTY_CODE == 37) %>% select("Site ID", "Site Name", year) %>% unique %>% rename(site_id = "Site ID", site_name = "Site Name")
# find the intersection between the sites present in 2004 and 2019
site.year <- with(sites, split(site_id, year))
both <- intersect(site.year[[1]], site.year[[2]])
print(both)
## [1] 60370002 60371103 60371201 60372005 60374002 60374004 60379033 60379034

There are eight monitors operated in 2004 and 2019.

# choose one that had a reasonable amount of data in each year
count <- pm %>%
         rename(site_id = "Site ID") %>%
         filter(site_id %in% both)

# count the number of observations at each monitor to see which ones have the most observations
group_by(count, site_id) %>% summarize(n = n())
## # A tibble: 8 × 2
##    site_id     n
##      <int> <int>
## 1 60370002   400
## 2 60371103  1428
## 3 60371201   586
## 4 60372005   288
## 5 60374002   484
## 6 60374004  1043
## 7 60379033   471
## 8 60379034   219

Here we can see that site_id = 60371103 contains the most data, so we will pick this site

# select monitor with site_id = 60371103
pmsub <- pm %>%
         rename(site_id = "Site ID") %>%
         filter(site_id== 60371103) %>%
         select(Date, year, pm2.5) %>%
         mutate(Date = as.Date(Date), yday = yday(Date))

# plot the time series data of PM2.5 for the monitor in both years
qplot(yday, pm2.5, data = pmsub, facets = . ~ year, xlab = "Day of the year")
## Warning: Removed 865 rows containing missing values (geom_point).

From the graph, I notice that the median levels of PM2.5 has decreased a little from 2004 to 2019. Also, the variation (spread) in the PM2.5 values in 2019 is smaller than it was in 2004, which suggests that not only are median levels of PM2.5 lower in 2019, but that there are fewer large spikes from day to day.